package com.nomi.blog.utils;

import java.util.Random;

public class RandomUtils {
    private static final String NUMBERS = "0123456789";
    private static final String ALPHABET_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String ALPHABET_LOWER = "abcdefghijklmnopqrstuvwxyz";

    private static final Random RANDOM = new Random();

    /**
     * 生成指定长度的纯数字随机验证码
     * @param length
     * @return
     */
    public static String generateCode(int length){
        if(length <= 0){
            throw new IllegalArgumentException("验证码长度必须大于0");
        }

        StringBuilder code = new StringBuilder();
        for(int i = 0; i < length; i++){
            code.append(NUMBERS.charAt(RANDOM.nextInt(NUMBERS.length())));
        }

        return code.toString();
    }
}
